package com.example.mvp.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.baselibrary.R;
import com.example.mvp.bean.BannerDataEntity;
import java.util.ArrayList;

/**
 * 新闻Banner适配器
 * @author chaohaipeng  on 2018/4/19.
 */
public class NewBannerAdpter extends PagerAdapter {
    /**
     * 数据
     */
    private ArrayList<BannerDataEntity> mData;
    /**
     * 上写文
     */
    private Activity mActivity;
    /**
     * 图片请求操作
     */
    private final RequestOptions mRequestOptions;

    public NewBannerAdpter(Activity activity, ArrayList<BannerDataEntity> listData){
        mData=listData;
        mActivity=activity;
        mRequestOptions = new RequestOptions();
        mRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
        mRequestOptions.skipMemoryCache(true);
        mRequestOptions.error(R.drawable.icon_banner);

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Integer.MAX_VALUE-1;
    }
    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
        return POSITION_NONE;
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // 先移除在添加，更新图片在container中的位置（把iv放至container末尾）
        View view= LayoutInflater.from(mActivity).inflate(
                R.layout.viewpager_item, null);
        ImageView img =  view.findViewById(R.id.item_pager_image);
        if (mData.size()==0){
            return view;
        }
        final int newPosition = position % mData.size();
        RequestManager mRequestManager = Glide.with(mActivity);
        mRequestManager.load(mData.get(newPosition).getUrl())
                .thumbnail(0.2f)
                .apply(mRequestOptions)
                .into(img);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        container.addView(view);
        return view;
    }
}
